import React from 'react';
import {connect} from 'dva'
import * as apis from '../services/example'

class IndexPage extends React.Component {

    componentDidMount() {
        // apis.testCnode().then(res => {
        //   console.log(res)
        // })
        apis.mockData().then(res => {
            console.log(res)
        })
    }

    handleSetName = () => {
        this.props.dispatch({
            type: "indexTest/setName",
            data: {
                name: "亚斯娜"
            }
        })
    }
    handleSetNameAsync = () => {
        this.props.dispatch({
            type: "indexTest/setNameAsync",
            data: {
                name: "saber酱"
            }
        })
    }

    testCnode = () => {
        this.props.dispatch({
            type: "indexTest/testCnode",
        })
    }

    render() {
        return (<div>
            <div>我是首页
                <hr/>
                {this.props.msg}
                <hr/>
                你的名字: {this.props.name}
            </div>
            <button onClick={this.handleSetName}>setName</button>
            <button onClick={this.handleSetNameAsync}>setNameAsync</button>
            <button onClick={this.testCnode}>testCnode</button>
            <div>
                {
                    this.props.cnodeDataList.map(data => {
                        return <p>{data.title}</p>
                    })
                }
            </div>
        </div>)
    }
}

const mapStateToProps = state => {
    console.log(state)
    return {
        msg: "给阿姨来一杯卡布奇诺", name: state.indexTest.name, cnodeDataList: state.indexTest.cnodeDataList
    };
}
export default connect(mapStateToProps)(IndexPage)
